import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-18
 * Time: 20:30
 */
public class DeleteJDBC {

    //"jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false"
    public static void main(String[] args) throws SQLException {
        //建立数据源
        DataSource dataSource = new MysqlDataSource();

        //设置url user  password

        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //建立连接
        Connection connection = dataSource.getConnection();

        //输入信息
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要删除的id :");
        int num = scan.nextInt();

        //构建sql

        String sql = "delete from student where id = ?";

        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setInt(1,num);

        int n = statement.executeUpdate();
        System.out.println("n = "+n);
        statement.close();
        connection.close();

    }
}
